package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    @GetMapping("/download")
    public ResponseEntity<Resource> download(@RequestParam String filename){
        // 从classpath加载模板文件
        Resource resource = new ClassPathResource("templates/" + filename);

        //检查文件是否存在
        if (!resource.exists()) {
            return ResponseEntity.notFound().build();
        }

        //设置相应头，告诉浏览器，这是要下载的文件
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=\"" + filename + "\"")
                .body(resource);
    }

    /**
     * 根据时间修改可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        log.info("根据时间修改可预约人数：{}",orderSetting);
        ordersettingService.updateNumberByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam String month){
        log.info("根据年月查询对应的预约设置信息：{}",month);
        List<OrderSetting> list = ordersettingService.getOrderSettingByMonth(month);
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
    }

    /**
     * 文件上传
     * @return
     */
    @PostMapping("/upload")
    public Result upload(
            @RequestPart("excelFile") MultipartFile file) {

        log.info("收到文件上传请求，文件名：{}", file.getOriginalFilename());

        // 参数校验
        if (file == null) {
            log.error("文件参数绑定失败");
            return new Result(false, "请选择上传文件");
        }
        if (file.isEmpty()) {
            return new Result(false, "上传文件不能为空");
        }
        if (!"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                .equals(file.getContentType())) {
            return new Result(false, "仅支持.xlsx格式文件");
        }

        // 处理业务逻辑
        try {
            ordersettingService.processExcel(file.getInputStream());
            return new Result(true, "预约设置成功");
        } catch (IOException e) {
            log.error("文件流处理异常", e);
            return new Result(false, "文件处理失败");
        }
    }
}
